package com.spring.boot.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spring.boot.entity.Province;
import com.spring.boot.entity.TitleDate;
import com.spring.boot.mapper.ProvinceMapper;
import com.spring.boot.service.ProvinceService;
import com.spring.boot.service.TitleDateService;
import com.spring.boot.vo.ProvinceVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ProvinceServiceImpl extends ServiceImpl<ProvinceMapper, Province> implements ProvinceService {
    @Autowired
    private ProvinceMapper provinceMapper;
    @Autowired
    private TitleDateService titleDateService;

    @Override
    public void saveVo(ProvinceVo provinceVo) {
        Province province = new Province();
        BeanUtils.copyProperties(provinceVo,province);
        SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd");
        province.setCreateTime(matter.format(new Date()));
        super.saveOrUpdate(province);
    }

    @Override
    public List<TitleDate> listTitleDate(String date) {

        if (StrUtil.hasEmpty(date)){
            QueryWrapper<Province> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByDesc("create_time");
            List<Province> provinceList = provinceMapper.selectList(queryWrapper);
//        遍历title_date表，取到title
            List<TitleDate> titleDateList = titleDateService.list();
            for (TitleDate item :
                    titleDateList) {
                switch (item.getTitle()) {
                    case "新增确诊" :
                        item.setNumber(Integer.valueOf(provinceList.get(0).getConfirmedRelative()));
                        break;
                    case "新增本土" :
                        item.setNumber(Integer.valueOf(provinceList.get(0).getNativeRelative()));
                        break;
                    case "新增境外" :
                        item.setNumber(Integer.valueOf(provinceList.get(0).getOverseasInputRelative()));
                        break;
                    case "新增无症状" :
                        item.setNumber(Integer.valueOf(provinceList.get(0).getAsymptomaticRelative()));
                        break;
                    case "累计确诊" :
                        item.setNumber(Integer.valueOf(provinceList.get(0).getConfirmed()));
                        break;
                    case "累计治愈" :
                        item.setNumber(Integer.valueOf(provinceList.get(0).getCrued()));
                        break;
                    case "现有确诊" :
                        item.setNumber(Integer.valueOf(provinceList.get(0).getCurConfirm()));
                        break;
                    case "累计死亡" :
                        item.setNumber(Integer.valueOf(provinceList.get(0).getDied()));
                        break;
                }
            }
            return titleDateList;
        }
        else {
            List<Province> provinceList = provinceMapper.selectList(new QueryWrapper<Province>().eq("create_time", date));
            //        遍历title_date表，取到title
            List<TitleDate> titleDateList = titleDateService.list();
            for (TitleDate item :
                    titleDateList) {
                switch (item.getTitle()) {
                    case "新增确诊" :
                        item.setNumber(Integer.valueOf(provinceList.get(0).getConfirmedRelative()));
                        break;
                    case "新增本土" :
                        item.setNumber(Integer.valueOf(provinceList.get(0).getNativeRelative()));
                        break;
                    case "新增境外" :
                        item.setNumber(Integer.valueOf(provinceList.get(0).getOverseasInputRelative()));
                        break;
                    case "新增无症状" :
                        item.setNumber(Integer.valueOf(provinceList.get(0).getAsymptomaticRelative()));
                        break;
                    case "累计确诊" :
                        item.setNumber(Integer.valueOf(provinceList.get(0).getConfirmed()));
                        break;
                    case "累计治愈" :
                        item.setNumber(Integer.valueOf(provinceList.get(0).getCrued()));
                        break;
                    case "现有确诊" :
                        item.setNumber(Integer.valueOf(provinceList.get(0).getCurConfirm()));
                        break;
                    case "累计死亡" :
                        item.setNumber(Integer.valueOf(provinceList.get(0).getDied()));
                        break;
                }
            }
            return titleDateList;

        }

    }

    @Override
    public Map getTrend() {
        HashMap<String, List<Object>> map = new HashMap<>();//创建结果集

        List<Integer> asymptomaticList = new ArrayList<>();
        List<Integer> diedList = new ArrayList<>();
        List<Integer> confirmedList = new ArrayList<>();
        List<Integer> cruedList = new ArrayList<>();
        List<String> dateList = new ArrayList<>();
        //查询数据库
        QueryWrapper<Province> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("asymptomatic","died","confirmed","crued","create_time").orderByDesc("create_time").last("limit 30");
        List<Province> provinceList = provinceMapper.selectList(queryWrapper);

        for (Province province : provinceList) {
            asymptomaticList.add(Integer.valueOf(province.getAsymptomatic()));
            diedList.add(Integer.valueOf(province.getDied()));
            confirmedList.add(Integer.valueOf(province.getConfirmed()));
            cruedList.add(Integer.valueOf(province.getCrued()));
            dateList.add(province.getCreateTime());
        }
        Collections.reverse(asymptomaticList);
        Collections.reverse(diedList);
        Collections.reverse(confirmedList);
        Collections.reverse(cruedList);
        Collections.reverse(dateList);
        map.put("asymptomatic", Collections.singletonList(asymptomaticList));
        map.put("died", Collections.singletonList(diedList));
        map.put("confirmed", Collections.singletonList(confirmedList));
        map.put("crued", Collections.singletonList(cruedList));
        map.put("date", Collections.singletonList(dateList));

        return map;
    }
}
